package com.learn.tx.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.concurrent.TimeUnit;

@Repository
public class EmpDao {
    @Autowired
    JdbcTemplate jdbcTemplate;


    public void updateEmpNameById(Integer empId, String empName) {
        // try {
        //     TimeUnit.SECONDS.sleep(5);
        // } catch (InterruptedException e) {
        //     throw new RuntimeException(e);
        // }
        String sql = "update t_emp set emp_name=? where emp_id=?";
        jdbcTemplate.update(sql, empName, empId);


        // 默认只针对运行时异常回滚，编译时异常不回滚
        // System.out.println(10 / 0);
        // new FileInputStream("aaa.aaa");
    }

    public void updateEmpSalaryById(Integer empId, Double empSalary) {
        String sql = "update t_emp set emp_salary=? where emp_id=?";
        jdbcTemplate.update(sql, empSalary, empId);
        System.out.println(10 / 0);
    }

    public String selectEmpNameById(Integer empId) {
        String sql = "select emp_name from t_emp where emp_id=?";

        String empName = jdbcTemplate.queryForObject(sql, String.class, empId);

        return empName;
    }

}
